home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d18 / intrfc4.arc / UTIL.PAS < prev   
Pascal/Delphi Source File  |  1990-12-23  |  2KB  |  92 lines

  1. unit util;
  2.  
  3. interface
  4.   uses dos;
  5.  
  6.   function add_offset(p:pointer; add:word):pointer;
  7.   function minw(i,j:word):word;
  8.   function maxw(i,j:word):word;
  9.   function word_at(var b:byte):word;
  10.   function read_file(filename: string;var buffer:pointer):word;
  11.   { Attempts to read a file into buffer; returns nil if there was a problem }
  12.   function roundup(n,r:word):word;
  13. implementation
  14.   function add_offset(p:pointer; add:word):pointer;
  15.   var
  16.     s,o:word;
  17.     new:pointer;
  18.   begin
  19.     { Normalize p }
  20.     s := seg(p^);
  21.     o := ofs(p^);
  22.     if o > $f then
  23.     begin
  24.       s := s + o shr 4;
  25.       o := o and $f;
  26.     end;
  27.     { Add new offset }
  28.     o := o + add;
  29.     add_offset := ptr(s,o);
  30.   end;
  31.  
  32.   function minw(i,j:word):word;
  33.   begin
  34.     if i<j then
  35.       minw := i
  36.     else
  37.       minw := j;
  38.   end;
  39.  
  40.   function maxw(i,j:word):word;
  41.   begin
  42.     if i<j then
  43.       maxw := j
  44.     else
  45.       maxw := i;
  46.   end;
  47.  
  48.   function word_at(var b:byte):word;
  49.   var
  50.     p:^byte;
  51.   begin
  52.     p := add_offset(@b,1);
  53.     word_at := word(b) + word(p^) shl 8;
  54.   end;
  55.  
  56.   function read_file(filename: string;var buffer:pointer):word;
  57.   { Attempts to read a file into buffer; returns nil if there was a problem }
  58.   var
  59.     f:file;
  60.     size : word;
  61.   begin
  62.     assign(f,filename);
  63.     read_file := 0;
  64.     buffer := nil;
  65.     {$i-} reset(f,1); {$i+}
  66.     if ioresult <> 0 then
  67.       exit;
  68.     if filesize(f) > 65521 then
  69.     begin
  70.       writeln('File ',filename,' too large.  File not read.');
  71.       exit;
  72.     end;
  73.     if maxavail < filesize(f) then
  74.     begin
  75.       writeln('Out of memory.  File ',filename,' not read.');
  76.       exit;
  77.     end;
  78.     getmem(buffer,filesize(f));
  79.     blockread(f,buffer^,filesize(f),size);
  80.     close(f);
  81.     read_file := size;
  82.   end;
  83.  
  84.   function roundup(n,r:word):word;
  85.   begin
  86.     roundup := r*((n+r-1) div r);
  87.   end;
  88.  
  89. end.
  90.  
  91.  
  92.